using UnityEngine;
using System.Collections;

public class MazeDraggable : MonoBehaviour 
{
	private Vector3 screenPoint;
	private Vector3 offset;
	
	private Ray ray;
	private RaycastHit hit;
	
	public Camera cam;
	 
	private int collisionCount = 0;
	
	public GameObject[] testObject;
	
	Vector3 curScreenPoint;
	Vector3 curPosition;
	
	bool move = false;
	
	void OnMouseDown()
	{
	    screenPoint = cam.WorldToScreenPoint(gameObject.transform.position);
	    offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
	}
 
	void OnMouseDrag()
	{
	    curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
		curPosition = cam.ScreenToWorldPoint(curScreenPoint);
		
//		transform.position = curPosition;		
		if( transform.parent.rotation.z == 0.0f )
		{
			Debug.Log("VERTICAL");
			transform.position = new Vector3 ( Mathf.Clamp(curPosition.x, transform.position.x, transform.parent.position.x), curPosition.y, transform.position.z);
		}
		if ( transform.parent.rotation.z > 0.0f )
		{
			Debug.Log("HORIZONTAL");
			transform.position = new Vector3 ( curPosition.x, Mathf.Clamp(curPosition.y, transform.position.y, transform.parent.position.y), transform.position.z);
		}
	}
	
	void OnTriggerEnter ( Collider hit )
	{
		if ( hit.gameObject.tag == "MazeColliders" )
		{
			transform.parent = hit.gameObject.transform;
		}
	}
	
	void FixedUpdate ()
	{
		Debug.Log(transform.position);
	}

}